#region Using

using System;
using System.IO;
using System.Collections.Generic;
using System.Xml;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using BlogEngine.Core;

#endregion

public partial class admin_Pages_links : System.Web.UI.Page
{
    Guid id = Guid.Empty;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!String.IsNullOrEmpty(Request.QueryString["id"]) && Request.QueryString["id"].Length == 36)
        {
            id = new Guid(Request.QueryString["id"]);
        }

        if (!Page.IsPostBack && !Page.IsCallback)
        {
            BindData(id);
        }

        grid.RowDeleting += new GridViewDeleteEventHandler(grid_RowDeleting);
        btnAdd.Click += new EventHandler(btnAdd_Click);
        btnAdd.Text = id == Guid.Empty ? Resources.labels.add : Resources.labels.update;
        valExist.ServerValidate += new ServerValidateEventHandler(valExist_ServerValidate);
        Page.Title = Resources.labels.blogroll;
    }

    private void valExist_ServerValidate(object source, ServerValidateEventArgs args)
    {
        args.IsValid = true;

        foreach (Link link in Link.Items)
        {
            if (link.Id != id && link.Title.Equals(txtTitle.Text.Trim(), StringComparison.OrdinalIgnoreCase))
                args.IsValid = false;
            if (link.Id != id && link.Slug.Equals(txtSlug.Text.Trim(), StringComparison.OrdinalIgnoreCase))
                args.IsValid = false;
        }
    }

    void btnAdd_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            Link link;

            if (Request.QueryString["id"] != null)
                link = BlogEngine.Core.Link.GetById(new Guid(Request.QueryString["id"]));
            else
                link = new Link();

            //clean up the data
            if (!string.IsNullOrEmpty(txtWebUrl.Text) && !txtWebUrl.Text.Contains("://"))
                txtWebUrl.Text = "http://" + txtWebUrl.Text;

            if (!string.IsNullOrEmpty(txtFeedUrl.Text) && !txtFeedUrl.Text.Contains("://"))
                txtFeedUrl.Text = "http://" + txtFeedUrl.Text;

            if (!string.IsNullOrEmpty(txtImageUrl.Text) && !txtImageUrl.Text.Contains("://"))
                txtImageUrl.Text = "http://" + txtImageUrl.Text;

            List<string> xfnTags = new List<string>();
            foreach (ListItem item in cblXfn.Items)
            {
                if (item.Selected)
                {
                    xfnTags.Add(item.Text);
                }
            }

            //create/update the record.
            link.Title = txtTitle.Text;
            link.Slug = txtSlug.Text;
            link.Description = txtDescription.Text;
            link.Feed = txtFeedUrl.Text;
            link.Website = txtWebUrl.Text;
            link.Image = txtImageUrl.Text;
            link.BlogRoll = cbBlogRoll.Checked;
            link.IsPublished = cbPublished.Checked;
            link.Order = int.Parse(txtOrder.Text);
            link.XFNTag = string.Join(";", xfnTags.ToArray());
            link.Save();

            Response.Redirect(Request.Path,true);
        }
    }

    void grid_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        Guid id = (Guid)grid.DataKeys[e.RowIndex].Value;
        Link link = Link.GetById(id);
        link.Delete();
        link.Save();
        Response.Redirect(Request.Path, true);
    }

    private void BindData(Guid categoryId)
    {
        BindLink(categoryId);
        BindGrid();
    }

    private void BindLink(Guid linkId)
    {
        BlogEngine.Core.Link link = new Link();
        
        if (linkId != Guid.Empty)
        {
            BlogEngine.Core.Link tempLink = BlogEngine.Core.Link.GetById(linkId); 

            if (tempLink != null)
                link = tempLink;
        }

        txtTitle.Text = link.Title;
        txtSlug.Text = link.Slug;
        txtDescription.Text = link.Description;
        txtFeedUrl.Text = link.Feed;
        txtWebUrl.Text = link.Website;
        txtImageUrl.Text = link.Image;
        cbBlogRoll.Checked = link.BlogRoll;
        txtOrder.Text = link.Order.ToString();
        cbPublished.Checked = link.IsPublished;

        string[] xfnTags = link.XFNTag.Split(';');

        foreach (string item in xfnTags)
        {
            foreach (ListItem listitem in cblXfn.Items)
            {
                if (listitem.Text == item)
                    listitem.Selected = true;
            }
        }
    }

    private void BindGrid()
    {
        grid.DataKeyNames = new string[] { "Id" };
        grid.DataSource = Link.Items;
        grid.DataBind();
    }
}
